package com.hafu.dao;

import com.hafu.model.RuKuMain;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class RuKuMainDao {
    public int addRuKuMain(RuKuMain ruku, Connection conn)throws SQLException{
        String sql="insert into ruku_main(ruku_name,price,gys_name,rk_date)values(?,?,?,current_timestamp)";
        PreparedStatement preparedStatement = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
        preparedStatement.setString(1,ruku.getRukuName());
        preparedStatement.setBigDecimal(2,ruku.getPrice());
        preparedStatement.setString(3,ruku.getGysName());
        int i=preparedStatement.executeUpdate();
        if (i>0){
            ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
            generatedKeys.next();
            return generatedKeys.getInt(1);
        }else {
            return -1;
        }
    }
}
